package com.chenhy.service;

import com.chenhy.mapper.ProviderMapper;
import com.chenhy.pojo.Provider;
import com.chenhy.pojo.User;
import com.chenhy.security.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProviderServiceImpl implements ProviderService {

    @Autowired
    ProviderMapper providerMapper;

    @Override
    @Cacheable(value = "provider", key = "'providerList'")
    public List<Provider> queryAllProvider() {
        return providerMapper.queryAllProvider();
    }

    @Override

    public List<Provider> userFuzzyQuery(String condition) {
        return providerMapper.userFuzzyQuery(condition);
    }

    @Override
    @CacheEvict(value = "provider", key = "'providerList'")
    public int addProvider(Provider provider) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        provider.setCreatedBy(loginUser.getId());
        provider.setModifyBy(loginUser.getId());
        return providerMapper.addProvider(provider);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "provider", key = "#id"),
            @CacheEvict(value = "provider", key = "'providerList'")
    })
    public int deleteProvider(int id) {
        return providerMapper.deleteProvider(id);
    }

    @Override
    @Cacheable(value = "provider", key = "#id")
    public Provider queryProviderById(int id) {
        return providerMapper.queryProviderById(id);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "provider", key = "#provider.id"),
            @CacheEvict(value = "provider", key = "'providerList'")
    })
    public int updateProvider(Provider provider) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        provider.setModifyBy(loginUser.getId());
        return providerMapper.updateProvider(provider);
    }


}
